package com.ecp.commons.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ecp.commons.bean.Region;
import com.ecp.commons.service.RegionService;

@Controller
public class RegionController {
	
	@Autowired
	private RegionService regionService;
	
	
	//获取30天的最低价信息
	static private final String req_region = "/flight/queryRegionByPid.do";
	
	@RequestMapping(req_region)
	public ModelAndView queryRegionByPid(HttpServletRequest request) {
		
		JSONObject json = new JSONObject();
		try {
			String id = ServletRequestUtils.getStringParameter(request, "id");
			
			List<Region> list = regionService.getChildren(Integer.valueOf(id));
			
			List<Map<String,String>> l = new ArrayList<Map<String,String>>();
			for(Region region : list ){
				Map<String,String> map = new HashMap<String,String>();
				map.put("value", String.valueOf(region.getId()) );
				map.put("name", region.getName());
				l.add(map);
			}
		
			json.put("list", JSONArray.toJSON(l));
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new ModelAndView("/json","json", json);
	}
}
